package com.weme.chamet.ui

import android.os.Bundle
import android.text.TextUtils
import androidx.constraintlayout.widget.ConstraintLayout
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.osom.proto.message.NetworkOuterClass
import com.weme.chamet.databinding.ActivitySplashBinding
import com.weme.common.arouter.ARouterHelper
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.common.manager.SocketConnectManager
import com.weme.common.network.JumpLoginBean
import com.weme.common.network.JumpMainBean
import com.weme.common.util.AgoraClient
import com.weme.common.util.AppMkHelper
import com.weme.common.util.LiveConfig.NET_WORK_LOGIN
import com.weme.common.util.UserMKHelper
import com.weme.common.util.downloadImageAsBitmap
import com.weme.common.util.fadeIn
import com.weme.common.util.isNetworkAvailable
import dev.kratos.api.cms_server.v1.KeyValueConfigReply
import dev.kratos.api.user_server.v1.CheckLoginReply
import dev.kratos.api.user_server.v1.CheckLoginRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class SplashActivity : BaseMvvmActivity<ActivitySplashBinding, SplashViewMode>() {


    override fun getViewModelClass(): Class<SplashViewMode> = SplashViewMode::class.java
    override fun initView(savedInstanceState: Bundle?) { //   ImmersionBar.with(this).init();
        mBinding.root.fadeIn()
        // 定义颜色数组
//        val colors = intArrayOf(
//            0xFF231313.toInt(), // #231313
//            0xFF240925.toInt(), // #240925
//            0xFF230623.toInt(), // #230623
//            0xFF1D131F.toInt()  // #1D131F
//        )
//
//        // 定义颜色位置的数组（范围是0到1）
//        val positions = floatArrayOf(
//            0f,     // 对应颜色 #231313
//            0.286f, // 对应颜色 #240925
//            0.806f, // 对应颜色 #230623
//            0.969f  // 对应颜色 #1D131F
//        )
//
//        // 创建一个GradientDrawable对象
//        val gradientDrawable = GradientDrawable(
//            GradientDrawable.Orientation.LEFT_RIGHT, // 渐变方向
//            colors // 颜色数组
//        ).apply {
//            gradientType = GradientDrawable.LINEAR_GRADIENT // 设置线性渐变
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//                setColors(colors, positions)
//            } // 设置颜色及其位置
//        }
//
//        // 将创建的渐变背景设置为ConstraintLayout的背景
//        mBinding.calView.background = gradientDrawable
        val displayMetrics = resources.displayMetrics
        val screenWidth = displayMetrics.widthPixels

        val layoutParams = mBinding.imLogo.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.width = (screenWidth * 0.240).toInt()
        layoutParams.height = layoutParams.width -10

        mBinding.imLogo.layoutParams = layoutParams
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init()
        if (isNetworkAvailable()) {
            SocketConnectManager.openService.observe(this@SplashActivity) {
                viewModel.getSevriceInfo()
            }
        } else {
            ARouterHelper.navigateToActivity(this@SplashActivity, ARouterPaths.LOGIN_ACTIVITY)
            finish()
        }
        CoroutineScope(Dispatchers.Default).launch {
         LiveEventBus.get<Any>(NET_WORK_LOGIN).observe(this@SplashActivity) {
                if (it is JumpLoginBean) {
                    ARouterHelper.navigateToActivity(this@SplashActivity, ARouterPaths.LOGIN_ACTIVITY)
                    finish()
                } else if (it is CheckLoginReply) {
                    if (!TextUtils.isEmpty(it.user.headPic) && TextUtils.isEmpty(UserMKHelper.decodeString(it.user.headPic))) {
                        UserMKHelper.encode(AppMkHelper.HEADPIC, it.user.headPic)
                        downloadImageAsBitmap(it.user.headPic) { it1 ->
                            if (null != it1) {
                                UserMKHelper.encode(it.user.headPic, it1)
                            }
                        }
                    }
                    loginAgora(it.user.uid.toString(), it.token)
                } else if (it is KeyValueConfigReply) {
                    viewModel.checkLoginApi(CheckLoginRequest.newBuilder().build())
                } else if (it is JumpMainBean) {
                    ARouterHelper.navigateToActivity(this@SplashActivity, ARouterPaths.MAIN_ACTIVITY)
                    finish()
                }
            }

        }

    }

    override fun onResume() {
        super.onResume()
        mBinding.lightView.startLight()
    }

    override fun onPause() {
        super.onPause()
        mBinding.lightView.stopLight()
    }


    private fun loginAgora(uid: String, token: String) {
        AgoraClient.getInstance().loginToAgora(uid, token) {
            if (it) {
                val fastLogin = NetworkOuterClass.LoginRequest.newBuilder().setSession(UserMKHelper.decodeString(UserMKHelper.SESSION)).build()
                viewModel.userLoginApi(fastLogin)
            } else {
                ARouterHelper.navigateToActivity(this@SplashActivity, ARouterPaths.LOGIN_ACTIVITY)
                finish()
            }
        }
    }


}